home *** CD-ROM | disk | FTP | other *** search
/ Nothing but Tetris / Nothing but Tetris.iso / amiga / workbench_tris / joy_input.c < prev    next >
C/C++ Source or Header  |  1992-11-19  |  5KB  |  159 lines

  1. /*
  2. Copyright (c) 1992, Trevor Smigiel.  All rights reserved.
  3.  
  4. (I hope Commodore doesn't mind that I borrowed their copyright notice.)
  5.  
  6. The source and executable code of this program may only be distributed in free
  7. electronic form, via bulletin board or as part of a fully non-commercial and
  8. freely redistributable diskette.  Both the source and executable code (including
  9. comments) must be included, without modification, in any copy.  This example may
  10. not be published in printed form or distributed with any commercial product.
  11.  
  12. This program is provided "as-is" and is subject to change; no warranties are
  13. made.  All use is at your own risk.  No liability or responsibility is assumed.
  14.  
  15. */
  16.  
  17. #include <exec/types.h>
  18. #include <exec/io.h>
  19. #include <exec/memory.h>
  20. #include <exec/exec.h>
  21. #include <dos/dos.h>
  22. #include <devices/gameport.h>
  23. #include <devices/inputevent.h>
  24. /* */
  25. #include <proto/exec.h>
  26.  
  27. #include "/GamePort_protos.h"
  28. #include "con_input.h"
  29.  
  30.  
  31. #define TIMEOUT 6
  32.  
  33. void
  34. JoyTask(void)
  35. {
  36.     struct GamePortTrigger joytrigger = {GPTF_UPKEYS | GPTF_DOWNKEYS, TIMEOUT, 1, 1};
  37.     struct IOStdReq *game_io_msg;
  38.     struct MsgPort *game_msg_port;
  39.     struct MsgPort *ReplyPort;
  40.     struct Task    *ThisTask;
  41.     struct ControlData *cd;
  42.  
  43.     Wait(SIGBREAKF_CTRL_F); /* Wait for parent to tell us to go */
  44.  
  45.     ThisTask = FindTask(NULL);
  46.     cd = (struct ControlData *) ThisTask->tc_UserData;
  47.  
  48.     ReplyPort = CreateMsgPort();
  49.     if (ReplyPort) {
  50.         game_msg_port = CreateMsgPort();
  51.         if (game_msg_port) {
  52.             game_io_msg = CreateIORequest(game_msg_port, sizeof(struct IOStdReq));
  53.             if (game_io_msg) {
  54.                 game_io_msg->io_Message.mn_Node.ln_Type = NT_UNKNOWN;
  55.                 if (!OpenDevice("gameport.device", 1, (struct IORequest *)game_io_msg, 0)) {
  56.                     if (set_controller_type(GPCT_ABSJOYSTICK, game_io_msg)) {
  57.  
  58.                         set_trigger_conditions(&joytrigger, game_io_msg);
  59.                         flush_buffer(game_io_msg);
  60.  
  61.                         {
  62.                             struct InputEvent game_event;        /* where input event
  63.                                                                  * will be stored */
  64.                             ULONG           signals,
  65.                                             signaled,
  66.                                             GameSig,
  67.                                             ReplySig;
  68.                             WORD            xmove,
  69.                                             ymove;
  70.                             struct ControlMsg CMsg;
  71.  
  72.                             CMsg.cm_Msg.mn_Node.ln_Type = NT_REPLYMSG;
  73.                             CMsg.cm_Msg.mn_ReplyPort = ReplyPort;
  74.                             CMsg.cm_Msg.mn_Length = sizeof(struct ControlMsg);
  75.                             CMsg.cm_CData = cd;
  76.  
  77.                             GameSig = (1L << game_msg_port->mp_SigBit);
  78.                             ReplySig = (1L << ReplyPort->mp_SigBit);
  79.                             signals =  GameSig | ReplySig | SIGBREAKF_CTRL_C;
  80.                             for (;;) {
  81.                                 send_read_request(&game_event, game_io_msg);
  82.  
  83.                                 signaled = Wait(signals);
  84.  
  85.                                 if (signaled & ReplySig) while (GetMsg(ReplyPort));
  86.  
  87.                                 if (signaled & SIGBREAKF_CTRL_C) {
  88.                                     if (signaled & GameSig)
  89.                                         while (GetMsg(game_msg_port));
  90.                                     break;
  91.                                 }
  92.  
  93.                                 if (signaled & GameSig) {
  94.                                     while (GetMsg(game_msg_port)) {
  95.                                         switch (game_event.ie_Code) {
  96.                                         case IECODE_LBUTTON:
  97.                                             cd->cd_State |= 0x10;
  98.                                             break;
  99.                                         case IECODE_RBUTTON:
  100.                                             cd->cd_State |= 0x20;
  101.                                             break;
  102.  
  103.                                         case (IECODE_LBUTTON | IECODE_UP_PREFIX):
  104.                                             cd->cd_State &= ~0x10;
  105.                                             break;
  106.                                         case (IECODE_RBUTTON | IECODE_UP_PREFIX):
  107.                                             cd->cd_State &= ~0x20;
  108.                                             break;
  109.  
  110.                                         case IECODE_NOBUTTON:
  111.                                             /* Check for change in position */
  112.                                             xmove = game_event.ie_X;
  113.                                             ymove = game_event.ie_Y;
  114.  
  115.                                             cd->cd_State &= ~15;
  116.                                             if (xmove == 1) {
  117.                                                 cd->cd_State |= 1;
  118.                                             } else if (xmove == -1) {
  119.                                                 cd->cd_State |= 2;
  120.                                             }
  121.                                             if (ymove == 1) {
  122.                                                 cd->cd_State |= 4;
  123.                                             } else if (ymove == -1) {
  124.                                                 cd->cd_State |= 8;
  125.                                             }
  126.                                             break;
  127.  
  128.                                         default:
  129.                                             break;
  130.                                         }
  131.                                     }
  132.                                     Forbid();
  133.                                     if ((cd->cd_State) && (CMsg.cm_Msg.mn_Node.ln_Type == NT_REPLYMSG)) {
  134.                                         CMsg.cm_State = cd->cd_State;
  135.                                         PutMsg(cd->cd_MsgPort, (struct Message *)&CMsg);
  136.                                     }
  137.                                     Permit();
  138.                                 }
  139.                             }
  140.                         }
  141.  
  142.                         if (!CheckIO((struct IORequest *)game_io_msg)) AbortIO((struct IORequest *)game_io_msg);
  143.                         WaitIO((struct IORequest *)game_io_msg);
  144.                         free_gp_unit(game_io_msg);
  145.                     }
  146.                     CloseDevice((struct IORequest *)game_io_msg);
  147.                 }
  148.                 DeleteIORequest(game_io_msg);
  149.             }
  150.             DeleteMsgPort(game_msg_port);
  151.         }
  152.         DeleteMsgPort(ReplyPort);
  153.     }
  154.     Forbid();
  155.     cd->cd_MsgPort = NULL;
  156.     Signal(cd->cd_Parent, SIGBREAKF_CTRL_F);
  157.     Wait(0L);
  158. }
  159.